package cn.wolfcode.p2p.base.controller;

import cn.wolfcode.p2p.anno.NeedLogin;
import cn.wolfcode.p2p.base.domain.SystemDictionaryItem;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.service.ISystemDictionaryService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.util.Constant;
import cn.wolfcode.p2p.util.JSONResult;
import cn.wolfcode.p2p.util.JSONResultUtil;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 用户基本信息
 */
@Controller
public class BasicInfoController {

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @Autowired
    private IUserInfoService userInfoService;

    @NeedLogin
    @RequestMapping("basicInfo")
    public String basicInfo(Model model){
        //需要查询出userinfo
        UserInfo userinfo = userInfoService.getUserInfo(UserContext.getLoginInfo().getId());
        model.addAttribute("userinfo",userinfo);
        //需要查询出明细列表
        List<SystemDictionaryItem> incomeGrades = systemDictionaryService.getItemBySn(Constant.ITEM_INCOMEGRADES);
        List<SystemDictionaryItem> educationBackgrounds = systemDictionaryService.getItemBySn(Constant.ITEM_EDUCATIONBACKGROUNDS);
        List<SystemDictionaryItem> marriages = systemDictionaryService.getItemBySn(Constant.ITEM_MARRIAGES);
        List<SystemDictionaryItem> kidCounts = systemDictionaryService.getItemBySn(Constant.ITEM_KIDCOUNTS);
        List<SystemDictionaryItem> houseConditions = systemDictionaryService.getItemBySn(Constant.ITEM_HOUSECONDITIONS);
        model.addAttribute("incomeGrades",incomeGrades);
        model.addAttribute("educationBackgrounds",educationBackgrounds);
        model.addAttribute("marriages",marriages);
        model.addAttribute("kidCounts",kidCounts);
        model.addAttribute("houseConditions",houseConditions);
        return "userInfo";
    }

    @NeedLogin
    @RequestMapping("basicInfo_save")
    @ResponseBody
    public Object update(UserInfo userInfo){
        userInfoService.updateUserInfo(userInfo);
        return JSONResultUtil.jsonResult();
    }
}
